package personal.jinhui.paprika.providers;

import java.util.HashMap;

import personal.jinhui.paprika.providers.UserDataMeta.AccountsTable;


import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
 
public class AccountsProvider extends ContentProvider {
    public static final UriMatcher uriMatcher;
    public static final int ACCOUNTS_COLLECTION = 1;
    public static final int ACCOUNTS_SINGLE = 2;
    private SqliteHelper sqliteHelper;
    static {
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(UserDataMeta.AUTHORIY, "accounts", ACCOUNTS_COLLECTION);
        uriMatcher.addURI(UserDataMeta.AUTHORIY, "accounts/#", ACCOUNTS_SINGLE);
    }
    public static HashMap<String , String> usersProjectionMap;
    static {
        usersProjectionMap = new HashMap<String , String>();
        usersProjectionMap.put(AccountsTable._ID, AccountsTable._ID);
        usersProjectionMap.put(AccountsTable.JID, AccountsTable.JID);
        usersProjectionMap.put(AccountsTable.PASSWORD, AccountsTable.PASSWORD);
        usersProjectionMap.put(AccountsTable.FULLJID, AccountsTable.FULLJID);
        usersProjectionMap.put(AccountsTable.AVATARID, AccountsTable.AVATARID);
    }
    @Override
    public int delete(Uri arg0, String arg1, String[] arg2) {
        return 0;
    }
    @Override
    public String getType(Uri uri) {
        switch (uriMatcher.match(uri)) {
        case ACCOUNTS_COLLECTION:
            return AccountsTable.CONTENT_TYPE;
        case ACCOUNTS_SINGLE:
            return AccountsTable.CONTENT_ITEM_TYPE;
        default:
            throw new IllegalArgumentException("Unknown URI" + uri);
        }
    }
    @Override
    public boolean onCreate() {
        sqliteHelper = new SqliteHelper(getContext(), UserDataMeta.DATABASE_NAME, null, UserDataMeta.DATABASE_VERSION);
        return true;
    }
    @Override
    public Cursor query(Uri uri, String[] projection, 
    		String selection, 
    		String[] selectionArgs, 
    		String sortOrder) {
        SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
        switch (uriMatcher.match(uri)) {
        case ACCOUNTS_COLLECTION:
            builder.setTables(AccountsTable.TABLE_NAME);
            builder.setProjectionMap(usersProjectionMap);
            break;
        case ACCOUNTS_SINGLE:
            builder.setTables(AccountsTable.TABLE_NAME);
            builder.setProjectionMap(usersProjectionMap);
            builder.appendWhere(AccountsTable._ID + "=" + uri.getPathSegments().get(1));
            break;
        default :
            builder.setTables(AccountsTable.TABLE_NAME);
            builder.setProjectionMap(usersProjectionMap);
            break;
        }
        String orderBy;
        if (TextUtils.isEmpty(sortOrder)) {
            orderBy = AccountsTable.DEFAULT_SORT_ORDER;
        } else {
            orderBy = sortOrder;
        }
        SQLiteDatabase db = sqliteHelper.getWritableDatabase();
        Cursor c = builder.query(db, projection, selection, selectionArgs, null, null, orderBy);
        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c;
    }
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        SQLiteDatabase db = sqliteHelper.getWritableDatabase();
        long rowId = db.insert(AccountsTable.TABLE_NAME, null, values);
        if (rowId > 0) {
            Uri insertUserUri = ContentUris.withAppendedId(AccountsTable.CONTENT_URI, rowId);
            getContext().getContentResolver().notifyChange(insertUserUri, null);
            return insertUserUri;
        }
        throw new SQLException("failed insert row into " + uri);
    }
    @Override
    public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
        return 0;
    }
}
